package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import negocio.Alumno;
import negocio.Cursada;
import negocio.Examen;
import negocio.NotaExamen;

public class NotaExamenDAO 
{
	private static NotaExamenDAO instancia;
	private final Connection c;
	
	private NotaExamenDAO()
	{
		c = BDManager.getInstance().getConexion();
	}

	public static NotaExamenDAO getInstance()
	{
		if(instancia ==null)
		{
			instancia = new NotaExamenDAO();
		}
		return instancia;
	}
	
	public void guardar(NotaExamen notaExamen)
	{
		String query = "INSERT INTO rinde(notaexamen, alumno_fk, examen_fk) VALUES (" + 
				notaExamen.getNota() + ",'"+ notaExamen.getAlumno().getDni() + "'," + notaExamen.getExamen().getId()+")";
		try {
			Statement s = c.createStatement();
			s.execute(query);
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<NotaExamen> getNotasExamenes(Examen examen)
	{
		String query = "SELECT * FROM rinde WHERE examen_fk="+ examen.getId();
		ArrayList<NotaExamen> res = new ArrayList<NotaExamen>();
		try {
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery(query);
			while(rs.next())
			{
				Alumno alumno = AlumnoDAO.getInstance().getAlumno(rs.getInt(3));
				res.add(new NotaExamen(rs.getInt(1), examen, alumno, rs.getInt(2)));
			}
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	

}
